/*
 * crc.c
 *
 *  Created on: Oct 16, 2013
 *      Author: Ken Arok
 */

#include "crc.h"


uint8_t vCompute_CRC7(uint8_t *_buf, uint8_t _size)
{
	uint8_t _crc, _value, i;

	_crc = 0;
	while (_size--) {
		_value = *_buf++;
		for (i = 0; i < 8; i++) {
			_crc <<= 1;
			if ((_value & 0x80) ^ (_crc & 0x80)) {
				_crc ^= 0x09;
			}
			_value <<= 1;
		}
	}
	_crc = (_crc << 1) | 1;
	return _crc;
}


uint16_t vUpdate_CRC_CCITT_Kermit(uint16_t _crc, uint8_t _data)
{
	_data ^= (uint8_t)_crc;
	_data ^= _data << 4;

	return((((uint16_t)_data << 8) | (uint8_t)(_crc >> 8)) ^ (uint8_t)(_data >> 4) ^ ((uint16_t)_data << 3));

}


uint16_t vCompute_CRC_CCITT_Kermit(uint16_t _length, uint8_t *_buf)
{
	uint16_t _val = 0;
	uint16_t i;

	for(i = 0; i < _length; i++) {
		_val = vUpdate_CRC_CCITT_Kermit(_val, _buf[i]);
	}

	return _val;
}
